package com.amap.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.amap.constants.Constants;
import org.springframework.util.StringUtils;

/**
 * @author liuyi
 * @since 2025/10/31 11:19
 */
public class SignUtils {
    private SignUtils() {
    }

    public static String buildSignContent(Map<String, String> paramMap) {
        if (paramMap != null && !paramMap.isEmpty()) {
            List<String> sortedKeys = new ArrayList(paramMap.keySet());
            Collections.sort(sortedKeys);
            StringBuilder content = new StringBuilder();

            for(int i = 0; i < sortedKeys.size(); ++i) {
                String key = (String)sortedKeys.get(i);
                if (!Constants.PARAM_SIGN.equals(key) && !Constants.PARAM_SIGN_TYPE.equals(key) && !Constants.PARAM_SIGN_ENCRYPT.equals(key)) {
                    String value = (String)paramMap.get(key);
                    if (key != null && !StringUtils.isEmpty(key) && value != null && !StringUtils.isEmpty(value)) {
                        if (i > 0) {
                            content.append("&");
                        }

                        content.append(key).append("=").append(value);
                    }
                }
            }

            return content.toString();
        } else {
            throw new IllegalArgumentException("待签名参数不能为空");
        }
    }

    public static String readStream(InputStream in) throws IOException {
        Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8);

        String var5;
        try {
            StringWriter writer = new StringWriter();

            try {
                char[] buffer = new char[4096];

                int len;
                while((len = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, len);
                }

                var5 = writer.toString();
            } catch (Throwable var8) {
                try {
                    writer.close();
                } catch (Throwable var7) {
                    var8.addSuppressed(var7);
                }

                throw var8;
            }

            writer.close();
        } catch (Throwable var9) {
            try {
                reader.close();
            } catch (Throwable var6) {
                var9.addSuppressed(var6);
            }

            throw var9;
        }

        reader.close();
        return var5;
    }

    public static byte[] decodeBase64(String base64Str) {
        if (StringUtils.isEmpty(base64Str)) {
            throw new IllegalArgumentException("Base64字符串不能为空");
        } else {
            return Base64.getDecoder().decode(base64Str.getBytes(StandardCharsets.UTF_8));
        }
    }

    public static String encodeBase64(byte[] bytes) {
        if (bytes != null && bytes.length != 0) {
            return Base64.getEncoder().encodeToString(bytes);
        } else {
            throw new IllegalArgumentException("待编码字节数组不能为空");
        }
    }
}
